﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace jnxcLogin
{
    public partial class SendPower : System.Web.UI.Page
    {
        Maticsoft.BLL.UserInfo um = new Maticsoft.BLL.UserInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["sendPId"] != null)
                {
                    int user_id = Convert.ToInt32(Session["sendPId"]);

                    Maticsoft.Model.UserInfo  u = um.GetModel(user_id);

                    this.lbl_userName.Text = u.loginName;
                    this.lbl_user_id.Text = u.user_id.ToString();

                    this.BindNotice(user_id);
                }
            }
            
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (this.ddl_sysName.SelectedValue == "-1")
            {
                Maticsoft.Common.MessageBox.Show(this.Page,"请选择授权系统");
                return;
            }

            string loginName = this.txt_trueName.Text;
            string trueName = this.txt_loginName.Text;

            if (this.ddl_sysName.SelectedValue == "防治系统")
            {
                string str = " 1=1 ";
                if (!string.IsNullOrEmpty(loginName))
                {
                    str += " and uname like '%"+loginName+"%' ";
                }
                if (!string.IsNullOrEmpty(trueName))
                {
                    str += " and utruename like '%"+trueName+"%' ";
                }

                DataSet ds = um.GetListFZUserInfo(str);

                this.gv_sysFZ.Visible = true;
                this.gv_sysCB.Visible = false;
                this.gv_sysJY.Visible = false;
                this.gv_sysKS.Visible = false;
                this.gv_sysSB.Visible = false;

                this.gv_sysFZ.DataSource = ds;
                this.gv_sysFZ.DataBind();
            }
            if (this.ddl_sysName.SelectedValue == "测报系统")
            {

                string str = " 1=1 ";
                if (!string.IsNullOrEmpty(loginName))
                {
                    str += " and uname like '%" + loginName + "%' ";
                }
                if (!string.IsNullOrEmpty(trueName))
                {
                    str += " and utruename like '%" + trueName + "%' ";
                }

                DataSet ds = um.GetListCBUserInfo(str);

                this.gv_sysFZ.Visible = false;
                this.gv_sysCB.Visible = true;
                this.gv_sysJY.Visible = false;
                this.gv_sysKS.Visible = false;
                this.gv_sysSB.Visible = false;

                this.gv_sysCB.DataSource = ds;
                this.gv_sysCB.DataBind();
            }
            if (this.ddl_sysName.SelectedValue == "设备监测")
            {

                string str = " 1=1 ";
                if (!string.IsNullOrEmpty(loginName))
                {
                    str += " and UserName like '%" + loginName + "%' ";
                }
                if (!string.IsNullOrEmpty(trueName))
                {
                    str += " and TrueName like '%" + trueName + "%' ";
                }

                DataSet ds = um.GetListSBUserInfo(str);

                this.gv_sysFZ.Visible = false;
                this.gv_sysCB.Visible = false;
                this.gv_sysJY.Visible = false;
                this.gv_sysKS.Visible = false;
                this.gv_sysSB.Visible = true;

                this.gv_sysSB.DataSource = ds;
                this.gv_sysSB.DataBind();
            }
        }
        //防治系统授权
        protected void gv_sysFZ_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_sq")
            {
                if (e.CommandArgument != null)
                {
                    int uid = Convert.ToInt32(e.CommandArgument);//防治系统uid
                    int user_id = 0;

                    if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                    {
                        user_id = Convert.ToInt32(this.lbl_user_id.Text);
                    }

                    Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                    if (u != null)
                    {
                        DataSet ds = um.GetList(" user_id <> " + user_id + " and sys_FZ_uid = "+ uid);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Maticsoft.Common.MessageBox.Show(this.Page,"此用户已授权给其他帐号，请取消授权后再次授权！");
                        }
                        else
                        {
                            u.syc_FZ = true;
                            u.sys_FZ_uid = uid;//防治系统uid

                            if (um.Update(u))
                            {
                                BindNotice(user_id);
                            }
                        }
                       
                    }
                }
            }

            if (e.CommandName == "lbn_qsq")
            {
                int uid = Convert.ToInt32(e.CommandArgument);//防治系统uid
                int user_id = 0;

                if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                {
                    user_id = Convert.ToInt32(this.lbl_user_id.Text);
                }

                Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                if (u != null)
                {
                    u.syc_FZ = false;
                    u.sys_FZ_uid = 0;//取消UID将uid设置为0

                    if (um.Update(u))
                    {
                        BindNotice(user_id);
                    }
                }
            }
        }
        //测报系统授权
        protected void gv_sysCB_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_sq")
            {
                if (e.CommandArgument != null)
                {
                    int uid = Convert.ToInt32(e.CommandArgument);//测报系统uid
                    int user_id = 0;

                    if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                    {
                        user_id = Convert.ToInt32(this.lbl_user_id.Text);
                    }

                    Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                    if (u != null)
                    {
                        DataSet ds = um.GetList(" user_id <> " + user_id + " and sys_CB_uid = " + uid);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Maticsoft.Common.MessageBox.Show(this.Page, "此用户已授权给其他帐号，请取消授权后再次授权！");
                        }
                        else
                        {
                            u.sys_CB = true;
                            u.sys_CB_uid = uid;//测报系统uid

                            if (um.Update(u))
                            {
                                BindNotice(user_id);
                            }
                        }
                       
                    }


                }
               
            }

            if (e.CommandName == "lbn_qsq")
            {
                int uid = Convert.ToInt32(e.CommandArgument);//测报系统uid
                int user_id = 0;

                if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                {
                    user_id = Convert.ToInt32(this.lbl_user_id.Text);
                }

                Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                if (u != null)
                {
                    u.sys_CB = false;
                    u.sys_CB_uid = 0;//取消授权将uid变为0

                    if (um.Update(u))
                    {
                        BindNotice(user_id);
                    }
                }
            }
        }

        //设备监测系统授权
        protected void gv_sysSB_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_sq")
            {
                if (e.CommandArgument != null)
                {
                    int kid = Convert.ToInt32(e.CommandArgument);//监测系统uid
                    int user_id = 0;

                    if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                    {
                        user_id = Convert.ToInt32(this.lbl_user_id.Text);
                    }

                    Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                    if (u != null)
                    {
                        DataSet ds = um.GetList("  " + " sys_SB_uid = " + kid + " and user_id <>" + user_id);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Maticsoft.Common.MessageBox.Show(this.Page, "此用户已授权给其他帐号，请取消授权后再次授权！");
                        }
                        else
                        {
                            u.sys_SB = true;
                            u.sys_SB_uid = kid;//测报系统uid

                            if (um.Update(u))
                            {
                                BindNotice(user_id);
                            }
                        }

                    }


                }

            }

            if (e.CommandName == "lbn_qsq")
            {
                int kid = Convert.ToInt32(e.CommandArgument);//监测系统uid
                int user_id = 0;

                if (!string.IsNullOrEmpty(this.lbl_user_id.Text))
                {
                    user_id = Convert.ToInt32(this.lbl_user_id.Text);
                }

                Maticsoft.Model.UserInfo u = um.GetModel(user_id);

                if (u != null)
                {
                    u.sys_SB = false;
                    u.sys_SB_uid = 0;//取消授权将uid变为0

                    if (um.Update(u))
                    {
                        BindNotice(user_id);
                    }
                }
            }
        }

        private void BindNotice(int user_id)
        {
            Maticsoft.Model.UserInfo u = um.GetModel(user_id);
            string str = "授权情况：";

            if (u.syc_FZ)
            {
                DataSet ds = um.GetListFZUserInfo(" uid =" + u.sys_FZ_uid);
                str += "已授权防治系统用户: " + ds.Tables[0].Rows[0]["utruename"] + " ,";
            }
            if (u.sys_CB)
            {
                DataSet ds = um.GetListCBUserInfo(" uid =" + u.sys_CB_uid);
                str += "已授权测报系统用户: " + ds.Tables[0].Rows[0]["utruename"] + " ,";
            }

            if (u.sys_SB)
            {
                DataSet ds = um.GetListSBUserInfo(" KeyId =" + u.sys_SB_uid);
                str += "已授权设备监测系统用户: " + ds.Tables[0].Rows[0]["TrueName"] + " ,";
            }

            this.lbl_notice.Text = str;
           

        }

       
    }
}